package com.resou.reader.activity.limitedrecharge;

import com.resou.reader.api.entry.ActivityRechargePrice;
import com.resou.reader.api.entry.RechargeActivityRule;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.ResouxsRepository;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.ToastUtil;

import java.util.List;

public class LimitedRechargePresenter extends ResouBasePresenter<LimitedRechargeContract.View> implements LimitedRechargeContract.Presenter {

    private final ResouxsRepository mRepository;

    LimitedRechargePresenter(LimitedRechargeContract.View view) {
        super(view);
        mRepository = ResouxsRepository.getInstance();
    }

    @Override
    public void loadActivityInfo() {
        addCompositeDisposable(
                mRepository.loadRechargeActivityInfo(UserInstance.getToken())
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                            int code = result.getCode();
                            if (code == 0) {
                                RechargeActivityRule data = result.getData();
                                mView.showActivityInfo(data.getActivityRules());
                                mView.showNetCountTime(data.getLastTime());
                            } else {
                                ToastUtil.makeShortToast(result.getMsg());
                            }
                        }, throwable -> mView.showError(throwable))
        );
    }

    public void checkUserIsPayInRechargeActivity(String price) {
        mView.showProgress("正在加载...");
        addCompositeDisposable(
                mRepository.checkUserIsPayInRechargeActivity(UserInstance.getToken())
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                //未参加过
                                mView.showPayDialog(price);
                            } else {
                                mView.showAlreadyPay(result.getMsg());
                            }
                        }, throwable -> {
                            mView.showAlreadyPay(throwable.getMessage());
                        })
        );
    }

    public void loadRechargePrice(int activityId) {
        addCompositeDisposable(
                mRepository.getRechargePrice(activityId)
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                            List<ActivityRechargePrice> data = result.getData();
                            mView.setPrices(data);
                        },throwable -> {

                        })
        );
    }
}
